﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using cocos2d;

namespace CampaignWP7
{
    class LogoLayer:CCLayer
    {
        public CCSprite pLogo;
        public ProgressBar pLogoProgressBar;
        private Thread thread;
        //
        public override bool init()
        {
            CCDirector.sharedDirector().deviceOrientation = 
                ccDeviceOrientation.CCDeviceOrientationPortrait;
            //
            pLogo = CCSprite.spriteWithFile(@"images/Logo");
            CCSize size = CCDirector.sharedDirector().getWinSize();
            pLogo.position = new CCPoint(size.width / 2, size.height / 2);
            addChild(pLogo,0);

            //
            pLogoProgressBar = new ProgressBar(100, 0);
            addChild(pLogoProgressBar,1);
            pLogoProgressBar.position = new CCPoint((size.width - pLogoProgressBar.width) / 2,
                                                     size.height / 2);
            return base.init();
        }
        //
        public void UpdateLogoLayer(float dt)
        {
            pLogoProgressBar.update(dt);
        }
        //
        public override void onEnter()
        {

            this.schedule(new SEL_SCHEDULE(this.UpdateLogoLayer));
            //
            thread = new Thread(new ThreadStart(loadRes));
            thread.Start();

            base.onEnter();
        }
        //
        public static CCScene scene()
        {

            CCScene scene = CCScene.node();

            CCLayer logoLayer = LogoLayer.node();

            scene.addChild(logoLayer);

            // return the scene
            return scene;
        }
        //
        public static new CCLayer node()
        {
            LogoLayer ret = new LogoLayer();
            if (ret.init())
            {
                return ret;
            }
            else
            {
                ret = null;
            }

            return ret;
        }
        //
        private void loadRes()
        {

            ResDef.initRes(out pLogoProgressBar.CurrentProgress);
            //
            CCDirector.sharedDirector().replaceScene(CampaignWP7Scene.scene());
        }
    }
}
